package org.itboys.mobile.service.mongo.mobile;


import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.random.RandomUtils;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.kitchen.util.MobileSignUtils;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayCode;
import org.itboys.mobile.util.PhoneUtil;
import org.itboys.mobile.util.SMS.DaYuSMSUtils;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_验证码_service
 */
@Service
public class SundayMobileCodeService extends BaseMongoService<SundayCode> {
    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayCode> getEntityClass() {
        return SundayCode.class;
    }
    /**
     * 发送验证码
     * @param mobile
     * @param type
     */
    public int sendCode(String mobile,Integer type){
        //2017年6月20日，新增，验证签名
        Map<String, Object> param =new HashMap<String,Object>();
        int resultCode=0;
        //校验手机号是否合法
        if(!PhoneUtil.isMobileNumber(mobile)){
            //   resultCode=;
            return -2;
        }
        //删除原有的验证码
        param.clear();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("mobile",mobile);

        super.delete(param);
        //保存验证码记录
        String code = RandomUtils.getIntRandom(6);
        super.save( new SundayCode(mobile,code,type));
        //发送验证码
        String typeStr="";

        if(type==SundayCommonConstants.validate_code_type_login){
            typeStr="会员登陆";
        }else if(type==SundayCommonConstants.validate_code_type_register) {
            typeStr = "会员注册陆";
        }else if(type==SundayCommonConstants.validate_code_type_update_password) {
            typeStr = "修改密码";
        }else if(type==SundayCommonConstants.validate_code_type_forget_password) {
            typeStr = "忘记密码";
        }else if(type==SundayCommonConstants.validate_code_type_set_pay_password) {
            typeStr = "设置支付密码";
        }else if(type==SundayCommonConstants.validate_code_type_update_pay_password) {
            typeStr = "修改支付密码";
        }else if(type==SundayCommonConstants.validate_code_type_sign_card) {
            typeStr = "绑定会员卡";
        }else{
            typeStr = "其他操作";
        }
            DaYuSMSUtils.sendSMS(mobile,code,typeStr);
        return resultCode;
    }

    /**
     *
     * @param mobile
     * @param type
     * @return
     */
    /**
     * 校验艳照门
     * @param mobile
     * @param code
     * @param type
     * @return
     */
    public int checkCode(String mobile,String code,Integer type){
        int resultCode=0;
        //校验手机号是否合法
        if(!PhoneUtil.isMobileNumber(mobile)){
            return -2;
        }
        //校验验证码
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("mobile",mobile);
        param.put("type",type);
        List<SundayCode> oldCodes = super.list(param);
        logger.info("oldCodes-size"+oldCodes.size());
        for(SundayCode c:oldCodes){
            logger.info("oldCodes-"+c.getCode()+",mobile="+c.getMobile());
        }
        if(ListUtil.isNull(oldCodes)){
            return -1;
        }
        String oldCode = oldCodes.get(0).getCode();
        logger.info("oldCode="+oldCode+",code="+code);
        //验证码错误
        if(!oldCode.equalsIgnoreCase(code)){
            return -3;
        }
        return resultCode;
    }


}
